
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList">
        <template slot="menuLeft">
          <el-button type="primary"  size="small" @click.stop="myAdd()">添加</el-button>
          <el-button type="primary" size="small" @click="notice">通知</el-button>
          <!-- <el-button
            v-if="epm_swexticketoperation_sendBatch"
            type="primary"
            size="small"
            hidden="true"
            @click="send"
          >发送短款</el-button>
          <el-button
            v-if="epm_swexticketoperation_cancelBatch"
            type="primary"
            size="small"
            hidden="true"
            @click="cancel"
          >撤销短款</el-button> -->
          <el-button v-if="epm_swexticketoperation_dels" type="primary" size="small" @click="del">删除</el-button>
          <el-button type="primary" size="small" v-if="false" @click="handleExport">导 出</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button v-if="scope.row.noticeStatus=='0'" type="text" size="small" plain @click="myEdit(scope.row)">编辑</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <open-dialog title="添加" :DialogVisible.sync="addVisableDialog">
      <el-form label-width="60px">
        <avue-form
          ref="form" 
          v-model="formData"
          :option="tableAddOption"
        >
        <template slot-scope="scope" slot="lineCode">
          <el-select v-model="formData.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCode">
          <el-select v-model="formData.stationCode" placeholder="请选择" style="width: 150px" clearable @change="changeStation" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationPersonName">
          <el-select v-model="formData.stationPersonName" placeholder="请选择" style="width: 150px" @change="(v)=>{formData.stationPersonName=v.nikename;formData.stationPersonCode=v.userCode}" clearable filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.nikename"
              :value="item">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationPersonCode">
          <el-select v-model="formData.stationPersonCode" placeholder="请选择" style="width: 150px" clearable :disabled="true"  filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.userCode"
              :value="item.userCode">
            </el-option>
          </el-select>
        </template>
        </avue-form>
        
        <div class="bottom-menu-input">
          <el-button type="primary" @click="save()">保存</el-button>
          <el-button @click="addVisableDialog = false"
            >取消</el-button
          >
        </div>
      </el-form>
    </open-dialog>

    <open-dialog title="编辑" :DialogVisible.sync="editVisableDialog">
      <el-form label-width="60px">
        <avue-form
          ref="form" 
          v-model="formData"
          :option="tableAddOption"
        >
        <template slot-scope="scope" slot="lineCode">
          <el-select v-model="formData.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCode">
          <el-select v-model="formData.stationCode" placeholder="请选择" style="width: 150px" clearable @change="changeStation" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationPersonName">
          <el-select v-model="formData.stationPersonName" placeholder="请选择" style="width: 150px" @change="(v)=>{formData.stationPersonName=v.nikename;formData.stationPersonCode=v.userCode}" clearable filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.nikename"
              :value="item">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationPersonCode">
          <el-select v-model="formData.stationPersonCode" placeholder="请选择" style="width: 150px" clearable :disabled="true"  filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.userCode"
              :value="item.userCode">
            </el-option>
          </el-select>
        </template>
        </avue-form>
        
        <div class="bottom-menu-input">
          <el-button type="primary" @click="audit()">保存</el-button>
          <el-button @click="editVisableDialog = false"
            >取消</el-button
          >
        </div>
      </el-form>
    </open-dialog>

  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {
  noticeBatch,
  sendBatch,
  cancelBatch,
  exportExcel,
  delBatch,
  addObj,
  getObj,
  putObj
} from "@/api/subwayepm/swexticketoperation";
import {
  tableAddOption
} from "@/const/crud/subwayepm/swexticketoperation";
import {
  getgetStationCodes,
  getLineCodes,
  getLineCodesEpm,
  getSysUserToLS
} from "@/api/subwayRat/commonCheck";
export default {
  name: "swexticketoperation",
  mixins: [baseCrud('subwayepm/swexticketoperation')],
  data() {
    return {
      formData:[],
      addVisableDialog:false,
      editVisableDialog:false,
      tableAddOption: tableAddOption,
      lineCodes:[],
      stationCodes:[],
      conductorPersonNames:[],//售票员名称
      searchForm:{
        noticeStatus:'0'
      },
    }
  },
  created() {
    this.epm_swexticketoperation_sendBatch = this.permissions[
      "epm_swexticketoperation_sendBatch"
    ];
    this.epm_swexticketoperation_cancelBatch = this.permissions[
      "epm_swexticketoperation_cancelBatch"
    ];
    this.epm_swexticketoperation_dels = this.permissions[
      "epm_swexticketoperation_dels"
    ];
  },
  watch: {
    // "form.stationPersonName": function() {
    //   this.form.stationPersonCode = this.form.stationPersonName;
    // }
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.epm_swexticketoperation_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.epm_swexticketoperation_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.epm_swexticketoperation_edit,
          false
        )
      };
    }
  },
  methods: {
    myAdd() {
      for (var i in this.formData) {
        this.formData[i] = "";
      }
      this.addVisableDialog = true;
    },
    //添加
    save: function () {
      if (
        this.formData.lineCode == "" ||
        this.formData.stationCode == "" ||
        this.formData.stationPersonName == "" ||
        this.formData.machineType == "" ||
        this.formData.machineCode == "" ||
        this.formData.involveMonet == null ||
        this.formData.backTimeLimit == "" ||
        this.formData.diffCase == "" 
      ) {
        this.$message.info(
          "线路、站点、车站人员、设备类型、设备编号、涉及金额、反馈期限、差异描述为必填项！不能为空"
        );
        return;
      }
      addObj(this.formData)
        .then((data) => {
          this.$message.success("添加成功");
          this.addVisableDialog = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
    },
    //编辑回显
    myEdit(row){
      getObj(row.id)
        .then((response) => {
          let res = response.data.data;
          this.formData = res;
          Object.assign(this.formData,row);
          this.changeLine2(row.lineCode);
          this.changeStation(row.stationCode);
        })
        .then(() => {
          this.editVisableDialog = true;
        });
    },
    //编辑
    audit: function () {
      if (
        this.formData.lineCode == "" ||
        this.formData.stationCode == "" ||
        this.formData.stationPersonName == "" ||
        this.formData.machineType == "" ||
        this.formData.machineCode == "" ||
        this.formData.involveMonet == null ||
        this.formData.backTimeLimit == "" ||
        this.formData.diffCase == "" 
      ) {
        this.$message.info(
          "线路、站点、车站人员、设备类型、设备编号、涉及金额、反馈期限、差异描述为必填项！不能为空"
        );
        return;
      }
      putObj(this.formData)
        .then((data) => {
          this.$message.success("修改成功");
          this.editVisableDialog = false;
          this.getList(this.page);
          done();
        })
        .catch(() => {});
    },
    notice() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
         if(table.noticeStatus!='0'){
        this.$message.error("已通知,请勿重复通知");
        return false;
        }
        ids.push(table);
      }
      noticeBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("通知成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    send() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
         if(table.sendShortStatus=='1'){
        this.$message.error("已发送,请勿重复发送");
        return false;
        }
        ids.push(table);
      }
      sendBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("发送成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    cancel() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if(table.noticeStatus=='2'){
        this.$message.error("售票员已反馈，不能撤回");
        return false;
        }
        ids.push(table);
      }
      cancelBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("撤回成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    del() {
      if (
        this.$refs.crud.tableSelect.length < 1 ||
        this.$refs.crud.tableSelect.length > 10
      ) {
        this.$message.error("选中表数量不合法，数量最少1个或最多为10个");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.noticeStatus != 0) {
          this.$message.error("请检查所选数据，只有未通知状态的数据可以删除");
          return false;
        }
        if (table.noticeStatus == 0) {
          ids.push(table.id);
        }
      }
      delBatch(ids)
        .then(data => {
          if (data.code == "0") {
            this.$message.success("删除成功");
          }
          this.getList(this.page);
        })
        .catch(() => {
          //loading();
        });
    },
    changeLine(v){ //改变线路时
      getgetStationCodes(v).then(res=>{
        this.stationCodes=res;
        this.formData.stationCode='';
        this.formData.stationPersonName='';
        this.formData.stationPersonCode='';
      });
    },
    changeLine2(v){ //改变线路时
      getgetStationCodes(v).then(res=>{
        this.stationCodes=res;
      });
    },
    changeStation(v){
      let p = {
        line_code : this.formData.lineCode,
        station_code : v,
        belong_to_line:0
      };
      getSysUserToLS(p).then((res)=>{
        this.conductorPersonNames=res.data.data;
      });
    }
  },
  mounted() {
    getLineCodesEpm().then(res=>{ //获取线路
      this.lineCodes=res;
    });
  }
}
</script>
<style lang="scss" scoped>
.bottom-menu-input {
  text-align: center;
}
</style>